package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.Result;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;

import com.itheima.dto.DishDto;
import com.itheima.pojo.DishFlavor;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import com.itheima.service.impl.SetmealServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishS;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealService setmealService;

    //http://localhost:8080/dish/page?page=1&pageSize=10
    //http://localhost:8080/dish/page?page=1&pageSize=10&name=%E7%B1%B3%E9%A5%AD
    //排序：创建时间的倒序排列，单页最多显示10条
    @GetMapping("/page")
    public Result page(int page, int pageSize, String name) {

        //定义一个DishDto的数组
        List<Dish> pageDto = new ArrayList<>();

        //分页拿到数据
        IPage<Dish> pages = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.hasLength(name), Dish::getName, name).orderByDesc(Dish::getUpdateTime);
        dishS.page(pages, lqw);
        List<Dish> records = pages.getRecords();

        for (Dish dish : records) {
            //创建一个DishDto对象
            DishDto dishDto = new DishDto();
            //将dish对象赋给dishDto
            BeanUtils.copyProperties(dish, dishDto);
            //获取dish的CategoryId,通过category获取name
            Long categoryId = dish.getCategoryId();
            Category category = categoryService.getById(categoryId);
            //将name赋给dishDto
            dishDto.setCategoryName(category.getName());
            //将dishDto添加到集合中
            pageDto.add(dishDto);
        }
        pages.setRecords(pageDto);
        return Result.success(pages);
    }


    //    http://localhost:8080/dish
//    请求方法: POST
    @PostMapping
    public Result saveDishSingle(@RequestBody DishDto dishDto) {
        Result result = dishS.add(dishDto);
        return result;
    }

    //请求 URL: http://localhost:8080/dish?ids=1455509375459360769,1455502306081517570,
    @DeleteMapping
    public Result delete(Long[] ids) {

        dishS.removeByIds(Arrays.asList(ids));

        return Result.success("删除成功");
    }

    //请求 URL: http://localhost:8080/dish/1455851691323695106
    //请求方法: GET
    //数据回显
    @GetMapping("/{id}")
    public Result goBack(@PathVariable Long id) {

        Dish dish = dishS.getById(id);
        Long categoryId = dish.getCategoryId();
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        Category category = categoryService.getById(categoryId);
        String name = category.getName();

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(lqw);

        dishDto.setFlavors(list);
        dishDto.setCategoryName(name);

        return Result.success(dishDto);
    }

//    请求 URL: http://localhost:8080/dish
//    请求方法: PUT

    @PutMapping
    public Result update(@RequestBody DishDto dishDto) {

        Result result = dishS.updateSingle(dishDto);
        return result;
    }


    //    Request URL: http://localhost:8080/dish/list?categoryId=1397844263642378242
//    Request Method: GET
    //请求 URL: http://localhost:8080/dish/list?categoryId=1397844263642378242&status=1
    //请求方法: GET
    @GetMapping("/list")
    public Result lists(Long categoryId,int status) {
        ArrayList<DishDto> dishDtos = new ArrayList<>();
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId, categoryId).eq(Dish::getStatus, 1).orderByDesc(Dish::getSort);
        List<Dish> list = dishS.list(lqw);
        for (Dish dish : list) {
            DishDto dishDto = new DishDto();
            Long dishId = dish.getId();
            LambdaQueryWrapper<DishFlavor> qw =new LambdaQueryWrapper<>();
            qw.eq(DishFlavor::getDishId,dishId);
            List<DishFlavor> list1 = dishFlavorService.list(qw);
            BeanUtils.copyProperties(dish,dishDto);
            dishDto.setFlavors(list1);
            dishDtos.add(dishDto);
        }
        return Result.success(dishDtos);

    }

//    请求 URL: http://localhost:8080/dish/status/0?ids=1413384757047271425
//    请求方法: POST
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable int status,@RequestParam List<Long> ids){

        Dish dish = new Dish();
        dish.setStatus(status);
        LambdaQueryWrapper<Dish> lqw= new LambdaQueryWrapper<>();
        lqw.in(Dish::getId,ids);
        dishS.update(dish,lqw);
        return Result.success("修改成功");
    }
}
